<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Newsmodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
      'news_status' => 'news.news_status',
    );
    public static $likeField = array(
      'news_title' => 'news_title',
    );
    public static $sortField = array();

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getNewsListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('news');
		    if(!empty($assist['join_content'])) {
          $this->db->join('news_content','news_content.news_id=news.news_id','left');
        }
        $this->db->where('news_status !=',STATUS_DELETE);
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }
        if(!isset($assist['nocount'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);


        $this->db->order_by('news_flg_home','desc');
        $this->db->order_by('news_sort','desc');
        // $this->db->order_by('news_status','desc');
        $this->db->order_by('news.news_id','desc');
        if(isset($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(isset($assist['nocount'])) return $list;
        return array($list,$count);
    }

    public function getNewsSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('news');
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getSearchNewsList($content){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('news');
        $this->db->where('news_status',STATUS_ACTIVE);
        if(!empty($content)) {
          $this->db->where(' (news_title like \'%'.$content.'%\' or news_desc like \'%'.$content.'%\') ', NULL, FALSE);
        }
        // $this->db->order_by('news_flg_home','desc');
        $this->db->order_by('news_sort','desc');
        $this->db->order_by('news.news_id','desc');
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }

    public function getHomeNews() {
      $this->db->from('news');
      $this->db->where('news_flg_home',STATUS_ACTIVE);
      $this->db->limit(1);
      $query = $this->db->get();
      $res = $query->row_array();

      return $res;
    }

    public function getNews($news_id,$has_content,$active){
        $this->db->from('news');
        if($has_content) {
          $this->db->join('news_content','news_content.news_id=news.news_id','left');
        }
        $this->db->where('news.news_id',$news_id);
        if($active) {
          $this->db->where('news_status',STATUS_ACTIVE);
        }
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getNewsById($news_id){
        if(!is_array($news_id)) $news_id = array($news_id);
        if(empty($news_id)) return array();

        $this->db->from('news');
        $this->db->where_in('news_id',$news_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }

    public function getNewsContentListByNewsIds($news_ids, $select = '') {
      if(empty($news_ids)) return array();
      if(isset($assist['select'])) $this->db->select($assist['select']);
      $this->db->from('news_content');
      $this->db->where_in('news_id',$news_ids);

      $query = $this->db->get();
      $list = $query->result_array();

      return $list;
    }

    public function getNewsContentByNewsId($news_id){
        $this->db->from('news_content');
        $this->db->where_in('news_id',$news_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createNews($info){
        $this->db->insert('news',$info);
        $news_id = $this->db->insert_id();

        return $news_id;
    }

    public function createNewsContent($info){
        $this->db->insert('news_content',$info);
        $id = $this->db->insert_id();

        return $id;
    }

    public function createOrEditNewsContent($info){
      $this->db->from('news_content');
      $this->db->where_in('news_id',$info['news_id']);
      $this->db->limit(1);
      $query = $this->db->get();
      $res = $query->row_array();

      if(empty($res)) {
        $this->db->insert('news_content',$info);
        $id = $this->db->insert_id();
      } else {
        $id = $res['news_content_id'];
        $this->db->where('news_id',$info['news_id']);
        $this->db->update('news_content',$info);
      }

      return $id;
    }

    public function createNewsBatch($info){
        if (!empty($info)) {
            $this->db->insert_batch('news',$info);
        }
    }

/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editNews($info){
        if(isset($info['news_id'])) {
            $news_id = $info['news_id'];
            unset($info['news_id']);
            $this->db->where('news_id',$news_id);
            $this->db->update('news',$info);
        }
    }

    public function editNewsContent($info){
        if(isset($info['news_id'])) {
            $news_id = $info['news_id'];
            unset($info['news_id']);
            $this->db->where('news_id',$news_id);
            $this->db->update('news_content',$info);
        }
    }

    public function updateNewsHome($news_id) {
      $this->db->where('news_flg_home',STATUS_ACTIVE);
      $this->db->update('news',array('news_flg_home' => STATUS_DISABLE));
      $this->db->where('news_id',$news_id);
      $this->db->update('news',array('news_flg_home' => STATUS_ACTIVE));
    }


    public function updateNewsBatch($update_arr){
        if (!empty($update_arr)) {
            $this->db->update_batch('news',$update_arr,'news_id');
        }
    }

}
